﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;

namespace game_data
{
    /// <summary>
    /// 配置表管理器
    /// </summary>
    public class ConfigManager : SingletonTable<ConfigManager>
    {
        /// <summary>
        /// 配置表数量
        /// </summary>
        public int ConfigNum { get; private set; }

        private readonly string postfix = ".txt";

        private Dictionary<Type, IDictionary> dictAllTypeDict = new Dictionary<Type, IDictionary>();

        private Dictionary<Type, IList> dictAllTypeList = new Dictionary<Type, IList>();

        #region Interface

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            dictAllTypeDict.Clear();
            dictAllTypeList.Clear();
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <typeparam name="T">配置类型</typeparam>
        /// <param name="id">配置ID</param>
        /// <returns></returns>
        public T GetConfig<T>(int id) where T : ITableBase
        {
            if (!dictAllTypeDict.ContainsKey(typeof(T)))
            {
                return null;
            }
            Dictionary<int, T> dict = dictAllTypeDict[typeof(T)] as Dictionary<int, T>;
            if (!dict.ContainsKey(id))
            {
                Console.WriteLine(typeof(T).Name + "表中未找到ID为:" + id + "的数据！");
                return null;
            }
            return dict[id];
        }

        /// <summary>
        /// 检查ID是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CheckIDExists<T>(int id) where T : ITableBase
        {
            if (!dictAllTypeDict.ContainsKey(typeof(T)))
            {
                return false;
            }
            Dictionary<int, T> dict = dictAllTypeDict[typeof(T)] as Dictionary<int, T>;
            return dict.ContainsKey(id);
        }

        /// <summary>
        /// 获取所有配置列表(不可增减)
        /// </summary>
        /// <typeparam name="T">配置类型</typeparam>
        /// <returns></returns>
        public IReadOnlyList<T> GetAllConfigByOrigin<T>() where T : ITableBase
        {
            if (!dictAllTypeList.ContainsKey(typeof(T)))
            {
                return null;
            }
            List<T> result = dictAllTypeList[typeof(T)] as List<T>;
            return result;
        }

        /// <summary>
        /// 获取所有配置列表(可增删)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> GetAllConfigByClone<T>() where T : ITableBase
        {
            if (!dictAllTypeList.ContainsKey(typeof(T)))
            {
                return null;
            }
            List<T> list = dictAllTypeList[typeof(T)] as List<T>;
            List<T> result = new List<T>();
            for (int i = 0; i < list.Count; i++)
            {
                result.Add(list[i]);
            }
            return list;
        }

        /// <summary>
        /// 加载配置表
        /// </summary>
        /// <typeparam name="T">配置类型</typeparam>
        /// <param name="configName">配置表名称</param>
        public void Load<T>(string configName) where T : ITableBase, new()
        {
            ConfigNum++;
            Type type = typeof(T);
            if (dictAllTypeDict.ContainsKey(type))
            {
                return;
            }
            Dictionary<int, T> dictConfig = new Dictionary<int, T>();
            List<T> listConfig = new List<T>();
            DataTable dt = TableReader.ReadFile(MyConfig.WorkDir + configName + postfix);
            foreach (DataRow row in dt.Rows)
            {
                T config = new T();
                config.SetTable<T>(row);
                if (!dictConfig.ContainsKey(config.ID))
                {
                    dictConfig.Add(config.ID, config);
                }
                listConfig.Add(config);
            }
            dictAllTypeDict.Add(type, dictConfig);
            dictAllTypeList.Add(type, listConfig);
        }

        #endregion

        /// <summary>
        /// 获取属性名称
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetPropertyName(string data)
        {
            data = data.Trim();
            string[] nameList = data.Split('_');
            string name = "";
            for (int j = 0; j < nameList.Length; j++)
            {
                if (string.IsNullOrEmpty(nameList[j]))
                {
                    continue;
                }
                name = name + nameList[j].Substring(0, 1).ToUpper() + nameList[j].Substring(1);
            }
            return name;
        }

        /// <summary>
        /// 解析Bool
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private bool ParseBool(string data)
        {
            return data == "1" ? true : false;
        }
    }
}
